package cn.aitrox.ry.app.web.service.impl;

import cn.aitrox.ry.app.web.service.SeriesService;
import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.common.service.BaseService;
import cn.aitrox.ry.common.service.api.dto.KV;
import cn.aitrox.ry.common.service.api.dto.User;
import cn.aitrox.ry.constant.CommonConstant;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultSearchInDto;
import cn.aitrox.ry.service.anno.dto.task.TaskDto;
import cn.aitrox.ry.service.anno.dto.task.in.TaskQueryInDto;
import cn.aitrox.ry.service.anno.entity.AnnoResultEntity;
import cn.aitrox.ry.service.anno.enumtype.anno.AnnoResultStatusEnum;
import cn.aitrox.ry.service.anno.feign.AnnoResultFeign;
import cn.aitrox.ry.service.anno.feign.TaskFeign;
import cn.aitrox.ry.service.config.dto.CommonConfigDto;
import cn.aitrox.ry.service.config.feign.CommonConfigFeign;
import cn.aitrox.ry.service.user.dto.in.UserListInDto;
import cn.aitrox.ry.service.user.feign.UserFeign;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SeriesServiceImpl extends BaseService implements SeriesService {

    @Autowired
    @Lazy
    private UserFeign userFeign;

    @Autowired
    @Lazy
    private AnnoResultFeign annoResultFeign;

    @Autowired
    @Lazy
    private TaskFeign taskFeign;

    @Autowired
    private CommonConfigFeign commonConfigFeign;

    public List<List<String>> seriesTaskExport(List<String> seriesUids) {
        AnnoResultSearchInDto annoResultSearchIn = new AnnoResultSearchInDto();
        annoResultSearchIn.setSeriesUids(seriesUids);
        annoResultSearchIn.setPage(1);
        annoResultSearchIn.setPageSize(100000);
        CommonPageInfo<AnnoResultEntity> annoResultSearchResult = annoResultFeign.search(annoResultSearchIn);
        List<User> userList = null;
        List<TaskDto> taskList = null;
        CommonConfigDto commonConfig = null;

        List<AnnoResultEntity> annoResultList = annoResultSearchResult.getList();
        if (!ArrayUtil.isEmpty(annoResultList)) {
            List<Integer> taskIds = annoResultList.stream().filter(x -> x.getTaskId() > 0 && x.getUserId() > 0).map(AnnoResultEntity::getTaskId).distinct().collect(Collectors.toList());
            List<Integer> userIds = annoResultList.stream().filter(x -> x.getTaskId() > 0 && x.getUserId() > 0).map(AnnoResultEntity::getUserId).distinct().collect(Collectors.toList());

            taskList = taskFeign.query(new TaskQueryInDto(taskIds));
            userList = userFeign.list(new UserListInDto(userIds));

            commonConfig = commonConfigFeign.commonList(Arrays.asList("TASK_LABEL", "TASK_TARGET"));
        }
        return this.packSeriesTaskExportData(annoResultList, taskList, userList, commonConfig);
    }

    private List<List<String>> packSeriesTaskExportData(List<AnnoResultEntity> annoResultList, List<TaskDto> taskList, List<User> userList, CommonConfigDto commonConfig) {
        List<List<String>> data = new ArrayList<>();
        List<String> header = Arrays.asList("序列号", "任务类型", "任务ID", "任务名称", "用户", "任务标签", "任务目标", "是否废片");
        data.add(header);
        if (ArrayUtil.isEmpty(annoResultList)) {
            return data;
        }
        Map<Integer, TaskDto> taskMap = null == taskList ? new HashMap<>() : taskList.stream().collect(Collectors.toMap(TaskDto::getId, x -> x));
        Map<Integer, String> userMap = null == userList ? new HashMap<>() : userList.stream().collect(Collectors.toMap(User::getId, User::getUserName));

        List<KV> taskTargets = commonConfig.getTaskTargets();
        List<KV> taskLabels = commonConfig.getTaskLabels();

        Map<String, String> taskTargetMap = null == taskTargets ? new HashMap<>() : taskTargets.stream().collect(Collectors.toMap(KV::getValue, KV::getName));
        Map<String, String> taskLabelMap = null == taskLabels ? new HashMap<>() : taskLabels.stream().collect(Collectors.toMap(KV::getValue, KV::getName));


        for (AnnoResultEntity annoResult : annoResultList) {
            Integer taskId = annoResult.getTaskId();
            Integer userId = annoResult.getUserId();
            TaskDto task = taskMap.get(taskId);
            String targetValue = task.getTarget();
            String labelValue = task.getLabel();
            AnnoResultStatusEnum annoResultStatusEnum = AnnoResultStatusEnum.getEnumByStatus(annoResult.getStatus());

            String taskName = task.getName();
            String userName = null == userMap.get(userId) ? CommonConstant.EMPTY : userMap.get(userId);
            String taskTarget = null == taskTargetMap.get(targetValue) ? CommonConstant.EMPTY : taskTargetMap.get(targetValue);
            String taskLabel = null == taskLabelMap.get(labelValue) ? CommonConstant.EMPTY : taskLabelMap.get(labelValue);
            String isDiscard = "未知";
            switch (annoResultStatusEnum) {
                case SUBMIT:
                    isDiscard = "否";
                    break;
                case DISCARD:
                    isDiscard = "是";
                    break;
                default:
                    break;
            }

            data.add(Arrays.asList(annoResult.getSeriesUid(), "标注", taskId.toString(), taskName, userName, taskTarget, taskLabel, isDiscard));
        }

        return data;
    }


}
